package org.example.springbook.service;


import org.example.springbook.enums.BookStatusEnum;
import org.example.springbook.mapper.BookMapper;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;


    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer count=bookMapper.count();
        bookMapper.queryBookByPage(pageRequest.getOffset(),pageRequest.getPageSize());
        List<BookInfo> bookInfos =bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        for(BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
//            if(bookInfo.getStatus()==0){
//                bookInfo.setStatusCN("删除");
//            }else if (bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else {
//                bookInfo.setStatusCN("不可借阅");
//            }
        }

        return new PageResponse<BookInfo>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
           return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
